using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using NMollom.Internal;

namespace NMollom {
    public enum HamOrSpam {
        Ham = 1,
        Spam = 2,
        Unsure = 3
    }

    [Serializable]
    public class ContentAssesment {
        private readonly HamOrSpam spam;
        private readonly double quality;
        private readonly string sessionId;
        [NonSerialized]
        private readonly Mollom mollom;

        public ContentAssesment(ContentCheckResult result) {
            spam = (HamOrSpam)result.spam;
            quality = result.quality;
            sessionId = result.sessionId;
            mollom = null;
        }

        public ContentAssesment(ContentCheckResult result, Mollom mollom) {
            spam = (HamOrSpam)result.spam;
            quality = result.quality;
            sessionId = result.sessionId;
            this.mollom = mollom;
        }

        public HamOrSpam Spam {
            get { return spam; }
        }

        public double Quality {
            get { return quality; }
        }

        public string SessionId {
            get { return sessionId; }
        }

        public bool IsSpam() {
            return spam == HamOrSpam.Spam;
        }

        public bool IsUnsure() {
            return spam == HamOrSpam.Unsure;
        }

        public bool IsHam() {
            return spam == HamOrSpam.Ham;
        }

        public void SendFeedback(FeedbackType type) {
            if (mollom == null)
                throw new InvalidOperationException("No mollom instance attached.");

            SendFeedback(mollom, type);
        }

        public void SendFeedback(Mollom useMollom, FeedbackType type) {
            if (useMollom == null) 
                throw new ArgumentNullException("useMollom");
            useMollom.SendFeedback(this, type);
        }

        #region Image Captcha
        public ImageCaptcha GetImageCaptcha() {
            if (mollom == null)
                throw new InvalidOperationException("No mollom instance attached.");
            return GetImageCaptcha(mollom);
        }

        public ImageCaptcha GetImageCaptcha(IPAddress authorIp) {
            if (mollom == null)
                throw new InvalidOperationException("No mollom instance attached.");
            return GetImageCaptcha(mollom, authorIp);
        }

        public ImageCaptcha GetImageCaptcha(Mollom useMollom) {
            return GetImageCaptcha(useMollom, null);
        }

        public ImageCaptcha GetImageCaptcha(Mollom useMollom, IPAddress authorIp) {
            if (useMollom == null)
                throw new ArgumentNullException("useMollom");
            return useMollom.GetImageCaptcha(this, authorIp);
        }
        #endregion

        #region Audio Captcha
        public AudioCaptcha GetAudioCaptcha() {
            if (mollom == null)
                throw new InvalidOperationException("No mollom instance attached.");
            return GetAudioCaptcha(mollom);
        }

        public AudioCaptcha GetAudioCaptcha(IPAddress authorIp) {
            if (mollom == null)
                throw new InvalidOperationException("No mollom instance attached.");
            return GetAudioCaptcha(mollom, authorIp);
        }

        public AudioCaptcha GetAudioCaptcha(Mollom useMollom) {
            return GetAudioCaptcha(useMollom, null);
        }

        public AudioCaptcha GetAudioCaptcha(Mollom useMollom, IPAddress authorIp) {
            if (useMollom == null)
                throw new ArgumentNullException("useMollom");
            return useMollom.GetAudioCaptcha(this, authorIp);
        }
        #endregion

        public override string ToString() {
            return string.Concat("{", spam.ToString(), " - ", quality.ToString("0.00"), "}");
        }
    }
}
